package com.ruoyi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.entity.*;
import com.ruoyi.project.domain.entity.ProjectSaleLog;
import com.ruoyi.project.mapper.*;
import com.ruoyi.project.mapper.ProjectSaleLogMapper;
import com.ruoyi.project.service.IProjectSaleLogService;
import com.ruoyi.project.service.IProjectSaleLogService;
import com.ruoyi.project.service.IProjectSaleService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 配套施工Service业务层处理
 *
 * @author zzy
 * @date 2025-10-13
 */
@Service
public class ProjectSaleLogServiceImpl extends ServiceImpl<ProjectSaleLogMapper, ProjectSaleLog> implements IProjectSaleLogService {
    @Autowired
    private ProjectSaleLogMapper ProjectSaleLogMapper;

    @Resource
    private ISysDictDataService dictService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Resource
    private ProjectSaleMapper projectSaleMapper;

    @Resource
    private ProjectAccountingMapper accountingMapper;

    @Resource
    private ProjectMapper projectMapper;

    /**
     * 查询配套施工
     *
     * @param id 配套施工主键
     * @return 配套施工
     */
    @Override
    public ProjectSaleLog selectProjectSaleLogById(Long id) {

        return ProjectSaleLogMapper.selectById(id);
    }

    @Override
    public List<ProjectSaleLog> selectProjectSaleOne(ProjectSaleLog log) {
        //根据数量 出库日期 商品ID 管家婆项目  可以唯一确定一条记录
        LambdaQueryWrapper<ProjectSaleLog> queryWrapper = new LambdaQueryWrapper<ProjectSaleLog>();
        queryWrapper.eq(ProjectSaleLog::getHousekeeperProject, log.getHousekeeperProject());
        queryWrapper.eq(ProjectSaleLog::getAmount, log.getAmount());
        queryWrapper.eq(ProjectSaleLog::getGoodsId, log.getGoodsId());
        queryWrapper.eq(ProjectSaleLog::getOrders, log.getOrders());
        queryWrapper.orderByAsc(ProjectSaleLog::getUpdateTime);
        List<ProjectSaleLog> projectSaleLogs = ProjectSaleLogMapper.selectList(queryWrapper);
        return projectSaleLogs;
    }

    /**
     * 查询配套施工列表
     *
     * @param matching 配套施工
     * @return 配套施工
     */
    @Override
    public List<ProjectSaleLog> selectProjectSaleLogList(ProjectSaleLog matching) {
        LambdaQueryWrapper<ProjectSaleLog> queryWrapper = new LambdaQueryWrapper<ProjectSaleLog>();
        queryWrapper.eq(ProjectSaleLog::getHousekeeperProject, matching.getHousekeeperProject());
        queryWrapper.eq(ProjectSaleLog::getAmount, matching.getAmount());
        queryWrapper.eq(ProjectSaleLog::getGoodsId, matching.getGoodsId());
        queryWrapper.eq(ProjectSaleLog::getOrders, matching.getOrders());
        queryWrapper.orderByDesc(ProjectSaleLog::getAmount);
        return ProjectSaleLogMapper.selectList(queryWrapper);
    }

    /**
     * 新增配套施工
     *
     * @param sale 配套施工
     * @return 结果
     */
    @Override
    public int insertProjectSaleLog(ProjectSale sale) {
        //查询有没有利润核算信息 有则不让修改
        String housekeeperProject = sale.getHousekeeperProject();
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getHousekeeperProject, housekeeperProject);
        List<Project> projectListlist = projectMapper.selectList(projectWrapper);
        if (projectListlist != null && projectListlist.size() > 0) {
            LambdaQueryWrapper<ProjectAccounting> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProjectAccounting::getProjectNo, projectListlist.get(0).getProjectNo());
            List<ProjectAccounting> accountingList = accountingMapper.selectList(wrapper);
            if (accountingList != null && accountingList.size() > 0) {
                throw new RuntimeException("该项目已添加利润计算信息,不能再次修改成本");
            }
        }


        //原始成本
        BigDecimal oldPrice = sale.getOldCostPrice();
        //新成本
        BigDecimal newPrice = sale.getCostPrice();
        //比较是否相等
        boolean isChanged = oldPrice.compareTo(newPrice) != 0;
        if (isChanged) {
            //插入日志


            String nickName = SecurityUtils.getLoginUser().getUser().getNickName();

            ProjectSaleLog saleLog = new ProjectSaleLog();
            saleLog.setOperator(nickName);
            saleLog.setCreateTime(DateUtils.getNowDate());
            saleLog.setHousekeeperProject(sale.getHousekeeperProject());
            saleLog.setAmount(-sale.getAmount());//取反 因为原始数据是取过反的
            saleLog.setGoodsId(sale.getGoodsId());
            saleLog.setSaleDate(sale.getSaleDate());
            saleLog.setOrders(sale.getOrders());

            saleLog.setOldCost(oldPrice);
            saleLog.setNewCost(newPrice);

            return ProjectSaleLogMapper.insert(saleLog);

        } else {
            return 1;
        }

    }

    /**
     * 修改配套施工
     *
     * @param ProjectSaleLog 配套施工
     * @return 结果
     */
    @Override
    public int updateProjectSaleLog(ProjectSaleLog ProjectSaleLog) {
        ProjectSaleLog.setUpdateTime(DateUtils.getNowDate());
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        ProjectSaleLog.setOperator(nickName);
        return ProjectSaleLogMapper.updateById(ProjectSaleLog);
    }

    /**
     * 批量删除配套施工
     *
     * @param ids 需要删除的配套施工主键
     * @return 结果
     */
    @Override
    public int deleteProjectSaleLogByIds(Long[] ids) {
        return ProjectSaleLogMapper.deleteProjectSaleLogByIds(ids);
    }

    /**
     * 删除配套施工信息
     *
     * @param id 配套施工主键
     * @return 结果
     */
    @Override
    public int deleteProjectSaleLogById(Long id) {
        return ProjectSaleLogMapper.deleteById(id);
    }

}
